package com.ysj.demo.accompanynetwork.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ysj.demo.accompanynetwork.ApiServiceUntil;
import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.util.SpUtil;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.bean.BaseBean;
import com.ysj.demo.accompanynetwork.bean.UserBean;
import com.ysj.demo.accompanynetwork.bean.UserEmailBean;
import com.ysj.demo.accompanynetwork.bean.UserInfoResult;
import com.ysj.demo.accompanynetwork.bean.UserPhoneBean;
import com.ysj.demo.accompanynetwork.bean.UserValidBean;
import com.ysj.demo.accompanynetwork.util.ToolUtil;
import com.ysj.demo.accompanynetwork.weight.ConfirmDialog;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonalCenterActivity extends BaseActivity implements View.OnClickListener {
    private TextView titleTv;
    private ImageView imgIv;
    private TextView nickTv, emailTv, phoneTv, validTv;
    private TextView updateEmailTv, updatePhoneTv, updateValidTv, updatePasswordTv;

    private String userId;
    private String sessionId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        StatusBarUtil.fullScreen(this);

        initView();
        initData();
    }

    private void initView() {
        titleTv = findViewById(R.id.top_tv);
        titleTv.setText(getResources().getString(R.string.personal_center));
        imgIv =  findViewById(R.id.user_image_iv);
        nickTv = findViewById(R.id.nick_tv);
        emailTv = findViewById(R.id.email_tv);
        phoneTv = findViewById(R.id.phone_tv);
        validTv = findViewById(R.id.valid_tv);
        updateEmailTv = findViewById(R.id.update_email_tv);
        updateEmailTv.setOnClickListener(this);
        updatePhoneTv = findViewById(R.id.update_phone_tv);
        updatePhoneTv.setOnClickListener(this);
        updateValidTv = findViewById(R.id.update_valid_tv);
        updateValidTv.setOnClickListener(this);
        updatePasswordTv = findViewById(R.id.update_password_tv);
        updatePasswordTv.setOnClickListener(this);
    }

    private void initData() {
        userId = (String) SpUtil.getParam(getApplicationContext(), SpUtil.USER_ID, "");
        sessionId = (String) SpUtil.getParam(getApplicationContext(), SpUtil.SESSION_ID, "");
        if (TextUtils.isEmpty(userId)) {
            return;
        } else {
            UserBean bean = new UserBean(userId);
            Call<UserInfoResult> call = ApiServiceUntil.getInstance().getmApi().getUserInfo(sessionId, bean);
            call.enqueue(new Callback<UserInfoResult>() {
                @Override
                public void onResponse(Call<UserInfoResult> call, Response<UserInfoResult> response) {
                    UserInfoResult body = response.body();
                    if(!TextUtils.isEmpty(body.getData().getPortrait())){
                        Glide.with(PersonalCenterActivity.this).load(body.getData().getPortrait()).into(imgIv);
                    }
                    if (!TextUtils.isEmpty(body.getData().getUsername())) {
                        nickTv.setText(body.getData().getUsername());
                    }

                    if (!TextUtils.isEmpty(body.getData().getEmail())) {
                        emailTv.setText(body.getData().getEmail());
                    }

                    if (!TextUtils.isEmpty(body.getData().getPhone())) {
                        phoneTv.setText(body.getData().getPhone());
                    }

                    if (body.getData().getIdcardverifi() == 0) {
                        validTv.setText("未认证");
                    } else {
                        validTv.setText("已认证");
                    }

                    Log.i("PersonalCenterActivity", new Gson().toJson(body));
                }

                @Override
                public void onFailure(Call<UserInfoResult> call, Throwable t) {
                    Log.i("PersonalCenterActivity", t.toString());
                }
            });
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //修改邮箱
            case R.id.update_email_tv:
                UpdateEmailDialog(this, "修改邮箱", emailTv.getText().toString(),"请输入邮箱", "确定", "取消");
                break;
            //修改手机号
            case R.id.update_phone_tv:
                UpdatePhoneDialog(this, "修改手机", phoneTv.getText().toString(),"请输入手机", "确定", "取消");
                break;
            //修改认证
            case R.id.update_valid_tv:
                final UserValidBean valid = new UserValidBean(userId, "雷朋", "211481198401154411");
                Call<BaseBean> validCall =ApiServiceUntil.getInstance().getmApi().updateUserValid(sessionId, valid);
                validCall.enqueue(new Callback<BaseBean>() {
                    @Override
                    public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                        BaseBean body = response.body();
                        if (body.getCode() == 0) {
                            ToolUtil.ToastShow(getApplication(), "认证成功");
                            validTv.setText("已认证");
                        } else {
                            ToolUtil.ToastShow(getApplication(), "认证失败");
                            validTv.setText("未认证");
                        }
                        Log.i("PersonalCenterActivity", new Gson().toJson(body));
                    }

                    @Override
                    public void onFailure(Call<BaseBean> call, Throwable t) {
                        Log.i("PersonalCenterActivity", t.toString());
                    }
                });
                break;
        }
    }

    public void UpdateEmailDialog(final Activity context, String title, String data,String hint, String leftText, String rightText) {
        final ConfirmDialog confirmDialog = new ConfirmDialog(context, title, data,hint, leftText, rightText);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                // TODO Auto-generated method stub
                confirmDialog.dismiss();
                String data = confirmDialog.getData();
                if (TextUtils.isEmpty(data)) {
                    ToolUtil.ToastShow(getApplication(), "请填入内容");
                    return;
                }
                final UserEmailBean bean = new UserEmailBean(userId, data);
                Call<BaseBean> call =ApiServiceUntil.getInstance().getmApi().updateUserEmail(sessionId, bean);
                call.enqueue(new Callback<BaseBean>() {
                    @Override
                    public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                        BaseBean body = response.body();
                        if (body.getCode() == 0) {
                            ToolUtil.ToastShow(getApplication(), "邮箱更新成功");
                            emailTv.setText(bean.getEmail());
                        } else {
                            ToolUtil.ToastShow(getApplication(), "邮箱更新失败");
                        }
                        Log.i("PersonalCenterActivity", new Gson().toJson(body));
                    }

                    @Override
                    public void onFailure(Call<BaseBean> call, Throwable t) {
                        Log.i("PersonalCenterActivity", t.toString());
                    }
                });
            }

            @Override
            public void doCancel() {
                // TODO Auto-generated method stub
                confirmDialog.dismiss();
            }
        });
    }

    public void UpdatePhoneDialog(final Activity context, String title, String data, String hint, String leftText, String rightText) {
        final ConfirmDialog confirmDialog = new ConfirmDialog(context, title, data,hint, leftText, rightText);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                // TODO Auto-generated method stub
                confirmDialog.dismiss();
                String data = confirmDialog.getData();
                if (TextUtils.isEmpty(data)) {
                    ToolUtil.ToastShow(getApplication(), "请填入手机号");
                    return;
                }
                final UserPhoneBean phone = new UserPhoneBean(userId, data);
                Call<BaseBean> phoneCall = ApiServiceUntil.getInstance().getmApi().updateUserPhone(sessionId, phone);
                phoneCall.enqueue(new Callback<BaseBean>() {
                    @Override
                    public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                        BaseBean body = response.body();
                        if (body.getCode() == 0) {
                            ToolUtil.ToastShow(getApplication(), "号码更新成功");
                            phoneTv.setText(phone.getPhone());
                        } else {
                            ToolUtil.ToastShow(getApplication(), "号码更新失败");
                        }
                        Log.i("PersonalCenterActivity", new Gson().toJson(body));
                    }

                    @Override
                    public void onFailure(Call<BaseBean> call, Throwable t) {
                        Log.i("PersonalCenterActivity", t.toString());
                    }
                });
            }

            @Override
            public void doCancel() {
                // TODO Auto-generated method stub
                confirmDialog.dismiss();
            }
        });
    }
}
